//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

package org.prx.prp.utility;

import java.io.IOException;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;

public class StreamingMediaPlayer implements OnBufferingUpdateListener, OnCompletionListener, OnErrorListener, OnInfoListener, OnPreparedListener {
	  private MediaPlayer mediaPlayer;
	  private String current;
	   public StreamingMediaPlayer() {	
	    mediaPlayer = new MediaPlayer();
	    mediaPlayer.setOnBufferingUpdateListener(this);
	    mediaPlayer.setOnCompletionListener(this);
	    mediaPlayer.setOnErrorListener(this);
	    mediaPlayer.setOnInfoListener(this);
	    mediaPlayer.setOnPreparedListener(this);
	  }
	  
	  public void play(String url) {
		  this.current=url;
		 
	    new Thread(new Runnable() {
	      public void run() {	
	    	  startListening();
	      }
	    }).start();
	  }  

	  private void listen(final String url, boolean stream) throws IllegalArgumentException,
	      IllegalStateException, IOException {	    
	    String playUrl = url;
	    mediaPlayer.reset();
	    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
	    mediaPlayer.setDataSource(playUrl);
	    try {
	      mediaPlayer.prepare();
	    } catch (Exception e) {
	      Log.e("", e.getMessage(), e);
	    }
	  }

	  private void startListening() {
	    stopPlaying();
	    String url = current;
	     try {
			listen(url, true);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	    
	  }
	  
	  private void stopPlaying() {
	    
	    if(mediaPlayer.isPlaying()) {
	      mediaPlayer.stop();
	    }
	  }

	@Override
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		//Nothing as of now, to be used  when hooked to UI.
		
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		arg0.release();		
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		//Log.d("PRPAND","Error in playing");
		return false;
	}

	@Override
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
	      mediaPlayer.start();		
	}

}
